﻿/**
* jQuery EasyUI 1.4.3
* Copyright (c) 2009-2015 www.jeasyui.com. All rights reserved.
*
* Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt
* To use it on other terms please contact us at info@jeasyui.com
* http://www.jeasyui.com/license_commercial.php
*
* jQuery EasyUI combobox 扩展
* jeasyui.extensions.combobox.autoComplete.js
* 开发 .NET-上海-呆呆
* 由 落阳 整理完善
* 最近更新：2015-12-13
*
* 依赖项：
*   1、jquery.jdirk.js
*
* Copyright (c) 2015 ChenJianwei personal All rights reserved.
*/
(function ($) {

    $.util.namespace("$.fn.combobox.extensions");

    var delayer = {

        _timer: {},
        delayTillLast: function (id, fn, wait) {
            var $this = this;
            if ($this._timer[id]) {
                window.clearTimeout($this._timer[id]);
                delete $this._timer[id];
            }

            return $this._timer[id] = window.setTimeout(function () {
                fn && fn();
                delete $this._timer[id];
            }, wait);
        }
    };

    function initAutoComplete(t, opts) {
        if (!opts.autoComplete) { return; }

        var _onBeforeLoad = opts.onBeforeLoad;
        opts.onBeforeLoad = function (params) {
            var flag = true;
            if ($.isFunction(_onBeforeLoad)) { flag = _onBeforeLoad.call(this, params); }
            if (opts.allowRemote) {
                return flag;
            } else {
                return false;
            }
        };

        var _filter = opts.filter;
        opts.filter = function (input, row) {
            console.log("触发:filter，参数：" + input);
            //if ($.string.isNullOrWhiteSpace(input)) { return true; }
            //input = input.toUpperCase();

            //// 先判断显示值
            //var opts = $(this).combobox("options");
            //var text = row[opts.textField].toUpperCase();
            //if (text.indexOf(input) >= 0) {
            //    return true;
            //}

            //return false;
            return _filter.apply(this, arguments);
        };
    }

    function initializeExtensions(target) {
        var t = $(target),
            state = $.data(target, "combobox"),
            opts = state.options;
        initAutoComplete(t, opts);
    }

    var _combobox = $.fn.combobox;
    $.fn.combobox = function (options, param) {
        if (typeof options == "string") {
            return _combobox.apply(this, arguments);
        }
        options = options || {};
        return this.each(function () {
            var jq = $(this),
                isInited = $.data(this, "combobox") ? true : false,
                opts = isInited ? options : $.extend({},
                        $.fn.combobox.parseOptions(this),
                        $.parser.parseOptions(this, [
                            {
                                enablePinyin: "boolean"
                            }
                        ]), options);
            _combobox.call(jq, opts, param);
            if (!isInited) {
                initializeExtensions(this);
            }
        });
    };
    $.union($.fn.combobox, _combobox);





    var defaults = {

        // 扩展 easyui-combobox 的自定义属性，表示是否开启根据输入内容自动查找补全功能；
        // Boolean 值，默认为 false。
        // 注意，要该功能生效，easyui-combobox 的 editable（可编辑） 属性必须为 true 。
        // 另外，开启本功能时，会重写 filter 属性，将会覆盖自定义的 filter 。
        autoComplete: false
    };

    var methods = {

    };

    if ($.fn.combobox.extensions.defaults) {
        $.extend($.fn.combobox.extensions.defaults, defaults);
    } else {
        $.fn.combobox.extensions.defaults = defaults;
    }

    if ($.fn.combobox.extensions.methods) {
        $.extend($.fn.combobox.extensions.methods, methods);
    } else {
        $.fn.combobox.extensions.methods = methods;
    }

    $.extend($.fn.combobox.defaults, defaults);
    $.extend($.fn.combobox.methods, methods);

})(jQuery);